﻿
using GCC.Container.Abstracts;

using GCCGrpc.AuthCenter;

using Grpc.Core;

namespace System.AuthCenter
{
    /// <summary>
    /// 权限中心的GRPC服务
    /// </summary>
    [GCCGRPCServer]
    public class AuthCenterService : GrpcServer.GrpcServerBase
    {
        private readonly AbsGCCContext _context;
        public AuthCenterService(AbsGCCContext context)
        {
            _context = context;
        }
        /// <summary>
        /// 根据Token获取用户
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        public override Task<GetUserByTokenResponse> GetUserByToken(GetUserByTokenRequest request, ServerCallContext context)
        {
            return Task.Run(() =>
            {
                var userInfo = AuthManager.GetUserInfoByToken(this._context, request);
                if (userInfo == null)
                {
                    return new GetUserByTokenResponse();
                }
                var res = new GetUserByTokenResponse()
                {
                    Uid = userInfo.UID
                };
                userInfo.Roles.ForEach(item => res.Roles.Add(item));
                userInfo.Scopes.ForEach(item => res.Scopes.Add(item));
                return res;
            });
        }


        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        public override Task<LoginResponse> Login(LoginRequest request, ServerCallContext context)
        {
            return Task.Run(() =>
            {


                var userInfo = AuthManager.Login(this._context, request, out var token);
                if (userInfo == null)
                {
                    return new LoginResponse();
                }
                var res = new LoginResponse()
                {
                    Uid = userInfo.UID,
                    Token = token
                };
                userInfo.Roles.ForEach(item => res.Roles.Add(item));
                userInfo.Scopes.ForEach(item => res.Scopes.Add(item));
                return res;
            });
        }

        /// <summary>
        /// 设置用户,不存在则新增
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        public override Task<SetUserResponse> SetUser(SetUserRequest request, ServerCallContext context)
        {
            return Task.Run(() =>
            {
                var dbRes = AuthManager.SetUser(request);
                if (!dbRes)
                {
                    return new SetUserResponse();
                }
                var res = new SetUserResponse()
                {
                    Completed = true,
                };
                return res;
            });
        }

        /// <summary>
        /// 获取用户的所有登陆信息
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        public override Task<GetUserLoginInfosResponse> GetUserLoginInfos(GetUserLoginInfosRequest request, ServerCallContext context)
        {
            return Task.Run(() =>
            {
                var dbRes = AuthManager.GetUserLoginInfos(request);

                var res = new GetUserLoginInfosResponse();
                dbRes.ForEach(item =>
                {
                    res.LoginInfoItems.Add(new GetUserLoginInfosResponseItem()
                    {
                        OpenID = item.OpenID,
                        OpenKey = item.OpenKey,
                        RefreshToken = item.RefreshToken,
                        Token = item.Token,
                        Type = item.Type,
                        UID = item.UID,
                        UnionID = item.UnionID,
                        Created = item.Created.Ticks,
                        CreatedBy = item.CreatedBy,
                        Modified = item.Modified.Ticks,
                        ModifiedBy = item.ModifiedBy,
                    });
                });
                return res;
            });
        }
        /// <summary>
        /// 获取用户的角色
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        public override Task<GetUserRolesResponse> GetUserRoles(GetUserRolesRequest request, ServerCallContext context)
        {
            return Task.Run(() =>
            {
                var dbRes = AuthManager.GetUserRoles(request);

                var res = new GetUserRolesResponse();
                dbRes.ForEach(item =>
                {
                    res.Roles.Add(new GetUserRolesResponseItem()
                    {
                        Name = item.Name,
                        RoleID = item.RoleID,
                    });
                });
                return res;
            });
        }

        /// <summary>
        /// 设置用户的角色
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        public override Task<SetUserRolesResponse> SetUserRoles(SetUserRolesRequest request, ServerCallContext context)
        {
            return Task.Run(() =>
            {
                var dbRes = AuthManager.SetUserRoles(request);
                if (!dbRes)
                {
                    return new SetUserRolesResponse();
                }
                var res = new SetUserRolesResponse()
                {
                    Completed = true,
                };
                return res;
            });
        }

        /// <summary>
        /// 获取用户的请求范围
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        public override Task<GetUserScopesResponse> GetUserScopes(GetUserScopesRequest request, ServerCallContext context)
        {
            return Task.Run(() =>
            {
                var dbRes = AuthManager.GetUserScopes(request);

                var res = new GetUserScopesResponse();
                dbRes.ForEach(item =>
                {
                    res.Scopes.Add(new GetUserScopesResponseItem()
                    {
                        Name = item.Name,
                        ScopesID = item.ScopeID,
                    });
                });
                return res;
            });
        }

        /// <summary>
        /// 设置用户的请求范围
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        public override Task<SetUserScopesResponse> SetUserScopes(SetUserScopesRequest request, ServerCallContext context)
        {
            return Task.Run(() =>
            {
                var dbRes = AuthManager.SetUserScopes(request);
                if (!dbRes)
                {
                    return new SetUserScopesResponse();
                }
                var res = new SetUserScopesResponse()
                {
                    Completed = true,
                };
                return res;
            });
        }

        public override Task<LogoutResponse> Logout(LogoutRequest request, ServerCallContext context)
        {
            return Task.Run(() =>
            {
                AuthManager.Logout(_context, request.Token);
                var res = new LogoutResponse()
                {
                };
                return res;
            });
        }
    }
}
